ExpandoObject Convert to Json or Json to ExpandoObject

105

ExpandoObject Convert to Json or Json to ExpandoObject -

//  if you add the NewtonSoft.Json NuGet package to your project,
// you can convert your object to a string with this single line of code:
string strCust = JsonConvert.SerializeObject(cust, new ExpandoObjectConverter());

// To convert that string back to your original ExpandoObject, you'll use code like this:
cust = JsonConvert.DeserializeObject<ExpandoObject>(res, new ExpandoObjectConverter());

// This will do a great job of serializing your properties and will normally ignore any methods you added (probably what you want). 
// The one exception is if you have a method that refers to itself (as the example in my previous column did). 
// In that scenario the serialization code will blow up.

Comments

Submit
0 Comments